package com.jy.rhin.support.util;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.Certificate;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.springframework.util.Base64Utils;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.crypto.KeyUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.SM2;
import lombok.extern.slf4j.Slf4j;

/**
 * 生成证书
 */
@Slf4j
public class CertificateUtils {
    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    public static boolean verify(String cer,String data,String sign){
    	InputStream inputStream = new ByteArrayInputStream(cer.getBytes());
    	Certificate x509Certificate = KeyUtil.readX509Certificate(inputStream);
    	SM2 sm2 = SmUtil.sm2();
    	sm2.setPublicKey(x509Certificate.getPublicKey());
        return sm2.verify(data.getBytes(), Base64.decode(sign));
    }
    
    public static String sign(String cer,String digest,String priKey){
    	InputStream inputStream = new ByteArrayInputStream(cer.getBytes());
    	Certificate x509Certificate = KeyUtil.readX509Certificate(inputStream);
    	byte[] decode = Base64.decode(priKey);
    	SM2 sm2 = SmUtil.sm2();
    	sm2.setPublicKey(x509Certificate.getPublicKey());
    	sm2.setPrivateKey(KeyUtil.generatePrivateKey("SM2", decode));
        return Base64.encode(sm2.sign(digest.getBytes()));
    }
    
    public static String digest(String data){
    	try {
    		MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
            sha256.reset();
            sha256.update(data.getBytes("UTF-8"));
            byte[] md = sha256.digest();
            return Base64Utils.encodeToString(md);
		} catch (Exception e) {
			return null; 
		}
    	
    }
    
    public static String getPublicKey(String cer){
    	InputStream inputStream = new ByteArrayInputStream(cer.getBytes());
    	Certificate x509Certificate = KeyUtil.readX509Certificate(inputStream);
        return Base64.encode(x509Certificate.getPublicKey().getEncoded());
    }
    
    
    public static void main(String[] args) throws Exception {
    
    	 Certificate x509Certificate = KeyUtil.readX509Certificate(new FileReader("D:/tmp/cert/6b60371d30fc427d83e302893f831b62.crt").getInputStream());
    	   
    	   StringBuffer cerbuf = new StringBuffer();
    	   cerbuf.append("-----BEGIN CERTIFICATE-----\n");
           cerbuf.append(Base64.encode(x509Certificate.getEncoded()));
           cerbuf.append("\n-----END CERTIFICATE-----");
           String dig = "n2zFWKlfVl9sJLxvuA4nY00duZEhCtmcXt8X0NSC4iE=VSA971cj7+F0jrzn9m3KTxTJN7F184L7uomv009vT8E=7tSJXhYGXgK0tScvMYurm+dby+BpRsCFdZR8vGIGab8=";
           String pri = "MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQg6wDS5IGLORZcpIidTU3wRrwniKyY3sLbLant/uY7o/KgCgYIKoZIzj0DAQehRANCAATXj1+oQfjSExLcss1AtZIlhGKgZiPxEDTOVYHBgbPXILpgou5wRJaUF+m8NmIpiB/HbyUVk/U+NpMdjzbZ6pur";
           SM2 sm2 = SmUtil.sm2( KeyUtil.generatePrivateKey("SM2", Base64.decode(pri)), x509Certificate.getPublicKey());
           System.out.println(Base64.encode(sm2.sign(dig.getBytes())));
    	
    	System.out.println(cerbuf);
	}
   

     

}
